package vip.liux.backend.adapter.web.generator;

import org.springframework.web.bind.annotation.*;
import vip.liux.application.dots.ListResultDto;
import vip.liux.application.dots.PagedResultDto;
import vip.liux.application.dots.PagedResultRequestDto;
import vip.liux.backend.application.service.gen.GenTableService;
import vip.liux.backend.domain.models.gen.GenTable;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/generator/table")
public class TableController {

    private final GenTableService databaseService;

    public TableController(GenTableService databaseService) {
        this.databaseService = databaseService;
    }

    @PostMapping("/sync")
    public void sync(@RequestParam(required = false) Long databaseId) {
        databaseService.sync(databaseId);
    }

    @PostMapping("/sync/db")
    public void syncDb(@RequestParam List<String> tableNames) {
        databaseService.syncDb(tableNames);
    }

    @GetMapping
    public PagedResultDto<GenTable> pageList(PagedResultRequestDto input) {
        return databaseService.pageList(input);
    }

    @GetMapping("/{id}")
    public GenTable detail(@PathVariable Long id) {
        return databaseService.detail(id);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        databaseService.delete(id);
    }

    @GetMapping("/list")
    public ListResultDto<GenTable> list() {
        return databaseService.list();
    }

    @GetMapping("/preview/{tableId}")
    public Map<String, String> preview(@PathVariable("tableId") Long tableId) {
        return databaseService.preview(tableId);
    }
}
